package com.yanqu.road.server.manager.activity.fivehero;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.fivehero.FiveHeroConfig;
import com.yanqu.road.entity.activity.fivehero.config.*;
import com.yanqu.road.logic.bussiness.activity.FiveHeroBusiness;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.utils.RandomHelper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class CrossFiveHeroActivityGroup {

    private Logger logger = LogManager.getLogger(CrossFiveHeroActivityGroup.class.getName());

    private RandomHelper randomHelper = new RandomHelper();

    protected final Object lock = new Object();

    private FiveHeroConfig config;

    private Map<Integer, CrossFiveHeroActivity> groupMap = new ConcurrentHashMap<>();

    private ActivityInfo activityInfo;

    public CrossFiveHeroActivityGroup(ActivityInfo tmpActivityInfo) {
        activityInfo = tmpActivityInfo;

        // 读配置
        config = initConfig(tmpActivityInfo);
    }


    public ActivityInfo getActivityInfo() {
        return activityInfo;
    }

    /**
     * 生成一份配置
     */
    private FiveHeroConfig initConfig(ActivityInfo tmpActivityInfo) {
        FiveHeroConfig tmpConfig = new FiveHeroConfig();
        tmpConfig.setActivityInfo(tmpActivityInfo);
        List<Integer> activityIdList = new ArrayList<>();
        activityIdList.add(tmpActivityInfo.getActivityId());

        Map<Integer, FiveHeroTypeConfig> tmpTypeConfigMap = FiveHeroBusiness.getTypeConfigMap(tmpActivityInfo.getActivityId());
        Map<Integer, FiveHeroCityConfig> tmpCityConfigMap = FiveHeroBusiness.getCityConfigMap(tmpActivityInfo.getActivityId());
        Map<Integer, FiveHeroBuffConfig> tmpBuffConfigMap = FiveHeroBusiness.getBuffConfigMap(tmpActivityInfo.getActivityId());
        Map<Integer, FiveHeroEventConfig> tmpEventConfigMap = FiveHeroBusiness.getEventConfigMap(tmpActivityInfo.getActivityId());
        Map<Integer, FiveHeroBookConfig> tmpBookConfigMap = FiveHeroBusiness.getBookConfigMap(tmpActivityInfo.getActivityId());
        Map<Integer, FiveHeroBanditConfig> tmpBanditConfigMap = FiveHeroBusiness.getBanditConfigMap(tmpActivityInfo.getActivityId());
        Map<Integer, FiveHeroTechConfig> tmpTechConfigMap = FiveHeroBusiness.getTechConfigMap(tmpActivityInfo.getActivityId());
        Map<Integer, FiveHeroOrderConfig> tmpOrderConfigMap = FiveHeroBusiness.getOrderConfigMap(tmpActivityInfo.getActivityId());
        Map<Integer, FiveHeroTitleConfig> tmpTitleConfigMap = FiveHeroBusiness.getTitleConfigMap(tmpActivityInfo.getActivityId());

        // 其他配置
        Map<String, ActivityConfig> activityConfigMap = ActivityBussiness.getActivityConfigMap(activityIdList).get(tmpActivityInfo.getActivityId());
        tmpConfig.setActivityInfo(tmpActivityInfo);
        tmpConfig.initConfig(activityConfigMap,tmpTypeConfigMap,tmpCityConfigMap,tmpBuffConfigMap,tmpEventConfigMap,tmpBookConfigMap,tmpBanditConfigMap,tmpTechConfigMap,tmpOrderConfigMap,tmpTitleConfigMap);
        return tmpConfig;
    }

    public void timerTask() {
        for(CrossFiveHeroActivity gac : groupMap.values()){
            gac.timerTask();
        }
    }

    public CrossFiveHeroActivity getActivity(int groupId) {
        if (!groupMap.containsKey(groupId)) {
            synchronized (lock) {
                if (!groupMap.containsKey(groupId)) {
                    CrossFiveHeroActivity activity = new CrossFiveHeroActivity(activityInfo,config,groupId);
                    groupMap.put(groupId, activity);
                }
            }
        }
        return groupMap.get(groupId);
    }

    public List<CrossFiveHeroActivity> getActivityList() {
        return new ArrayList<>(groupMap.values());
    }

    public void save() {
        for(CrossFiveHeroActivity ac : groupMap.values()){
            ac.save();
        }
    }

    public void reloadConfig() {
        for(CrossFiveHeroActivity ac : groupMap.values()){
            ac.reloadConfig(config);
        }
    }

    public void repair() {
        for(CrossFiveHeroActivity ac : groupMap.values()){
            ac.repair();
        }
    }
}
